<template>
  <el-container class="model_index">
    <el-main class="model-tree" style="padding-bottom:10;padding-top:10">
      <el-card shadow="never" class="model-tree-left">
        <span style="font-size:14px;font-weight: 700;">表单分类</span>
        <el-tree
          ref="tree"
          class="treeitems"
          :data="categoryTreeList"
          :props="defaultProps"
          :default-expanded-keys="defaultList"
          :highlight-current="true"
          :check-on-click-node="true"
          :expand-on-click-node="true"
          node-key="id"
          style="margin-top: 10px;"
          :current-node-key="currentNodekey"
          @node-click="handleNodeClick"
        />
      </el-card>
      <div class="model-tree-right">
        <el-card shadow="never" class="tableCard">
          <vxe-toolbar :refresh="{ query: getList1 }">
            <template v-slot:buttons>
              <el-button type="primary" icon="el-icon-plus" @click="openDialog({id: ''})">新增</el-button>
            </template>
          </vxe-toolbar>
          <vxe-table
            ref="xTree"
            text-algin="center"
            border
            stripe
            resizable
            highlight-hover-row
            :loading="loading"
            :data="list"
            :auto-resize="true"
            :height="$store.getters.searchRow"
          >
            <vxe-table-column field="formCode" width="150" title="表单编码" />
            <vxe-table-column field="formName" width="200" title="表单名称" />
            <vxe-table-column field="formTypeName" width="120" align="center" title="表单类型" />
            <vxe-table-column field="remark" min-width="180" title="描述" />
            <vxe-table-column field="formVersion" width="100" align="center" title="版本号" />
            <vxe-table-column field="publishStatus" width="80" align="center" title="状态">
              <template slot-scope="scope">
                <el-switch v-model="scope.row.publishStatus" :inactive-value="0" :active-value="1" active-text inactive-text @change="changeState(scope)" />
              </template>
            </vxe-table-column>
            <vxe-table-column width="160" align="center" title="操作" fixed="right">
              <template slot-scope="scope">
                <el-button v-if="scope.row.publishStatus == 0" size="mini" type="text" @click="openDialog(scope.row)">编辑</el-button>
                <el-button size="mini" type="text" @click="openDialog(scope.row, 2)">查看</el-button>
                <el-button v-if="scope.row.publishStatus == 0" size="mini" type="text" @click="delFormDefine(scope.row.id)">删除</el-button>
                <el-button v-if="scope.row.publishStatus == 0" size="mini" type="text" @click="designDialog(scope.row)">设计</el-button>
                <el-button size="mini" type="text" @click="previewDialog(scope.row)">预览</el-button>
              </template>
            </vxe-table-column>
          </vxe-table>
          <vxe-pager
            :current-page.sync="searchParams.pageNum"
            :page-size.sync="searchParams.pageSize"
            :total="total"
            :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'FullJump', 'Total']"
            @page-change="getList"
          />
        </el-card>
      </div>
    </el-main>
    <el-dialog width="40%" v-if="dialogFormVisible" :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="form" label-position="right" :disabled="title === '查看'" :rules="rules" label-width="6rem">
        <el-row class="mb16">
          <el-col :span="24">
            <el-form-item label="表单分类" prop="categoryId">
              <el-input v-model="currentNodeName" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="表单编码" prop="formCode">
              <el-input v-model="form.formCode" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label=" 表单名称" prop="formName">
              <el-input v-model="form.formName" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="表单类型" prop="formType">
              <el-select v-model="form.formType">
                <el-option label="自行开发" :value="1" />
                <el-option label="在线设计" :value="2" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="mb16">
          <el-col :span="24">
            <el-form-item label="描述">
              <el-input v-model="form.remark" type="textarea" rows="3"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="title !== '查看'" type="primary" @click="editOradd">确 定</el-button>
      </div>
    </el-dialog>
  </el-container>
</template>

<script>
import { getCategoryTreeList } from '@/api/workflow/category'
import { getFormDefinePageList, addFormDefine, viewFormDefine, modifyFormDefine, delFormDefine, changeStatus } from '@/api/workflow/formDefine'

export default {
  data() {
    return {
      dialogFormVisible: false,
      title: '',
      form: {
        id: '',
        categoryId: '',
        formType: 1,
        formCode: '',
        formName: '',
        publishStatus: '',
        version: '',
        remark: ''
      },
      noForm: {},
      rules: {
        categoryId: [{ required: true, message: '请输入表单分类', trigger: 'blur' }],
        formType: [{ required: true, message: '请输入表单类型', trigger: 'blur' }],
        formCode: [{ required: true, message: '请输入表单编码', trigger: 'blur' }],
        formName: [{ required: true, message: '请输入表单名称', trigger: 'blur' }]
      },
      searchParams: {
        industry: 'mall',
        pageNum: 1,
        pageSize: 20
      },
      defaultProps: {
        children: 'children',
        label: 'name',
        value: 'id'
      },
      currentNodekey: '',
      currentNodeName: '',
      defaultList: [],
      categoryTreeList: [],
      total: 10,
      loading: false,
      list: []
    }
  },
  created() {
    this.noForm = JSON.parse(JSON.stringify(this.form))
    this.getCategoryTree()
  },
  methods: {
    getCategoryTree() {
      getCategoryTreeList().then((response) => {
        this.currentNodekey = response[0].id
        this.currentNodeName = response[0].name
        this.categoryTreeList = [...response]
        this.$nextTick(() => {
          this.$refs.tree.setCurrentKey(this.currentNodekey)
          this.handleNodeClick({ id: this.currentNodekey, name: this.currentNodeName})
        })
      })
    },
    handleNodeClick(data) {
      if (!data.children) {
        this.searchParams.categoryId = data.id
        this.searchParams.pageNum = 1
        this.currentNodekey = data.id
        this.currentNodeName = data.name
        this.getList()
      }
    },
    getList1() {
      this.getList()
    },
    // 重置参数
    refreshParams(params) {
      this.searchParams = params
      this.getList()
    },
    // 删除
    delFormDefine(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delFormDefine({ id }).then(res => {
          this.$message.success('删除成功!')
          this.getList()
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 打开弹窗
    openDialog(info, type) {
      if (info.id === '') {
        this.title = '新增'
        this.form = this.noForm
        this.form.categoryId = this.currentNodekey
        this.form.formType = 1
      } else {
        this.title = type === 2 ? '查看' : '编辑'
        viewFormDefine({ id: info.id }).then(res => {
          this.form = res
        })
      }
      this.dialogFormVisible = true
    },
    // 编辑或新增
    editOradd() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id === '') {
            addFormDefine(this.form).then(res => {
              this.$message.success('新增成功')
              this.dialogFormVisible = false
              this.getList()
            })
          } else {
            modifyFormDefine(this.form).then(res => {
              this.$message.success('编辑成功')
              this.dialogFormVisible = false
              this.getList()
            })
          }
        }
      })
    },
    getList() {
      getFormDefinePageList(this.searchParams).then(res => {
        this.total = res.total
        this.list = res.records
      })
    },
    changeState(scope) {
      changeStatus({ id: scope.row.id, status: scope.row.publishStatus }).then(res => {
        if (res) {
          scope.row.publishStatus === 0 ? this.list[scope.seq - 1].publishStatus = 0 : this.list[scope.seq - 1].publishStatus = 1
          this.$message.success('状态更新成功')
        } else {
          scope.row.publishStatus === 1 ? this.list[scope.seq - 1].publishStatus = 0 : this.list[scope.seq - 1].publishStatus = 1
          this.$message.error(res.msg)
        }
      })
    }
  }
}
</script>

<style>
.model_index {
  .model-tree {
    display: flex;
    .model-tree-left {
      margin-right: 20px;
      min-width: 200px;
      height: 640px;
      overflow-y: auto;
      .el-tree-node.is-current > .el-tree-node__content {
        background-color: #2F8DFB !important;
        color: white;
      }
    }
    .model-tree-right {
      width: calc(100% - 200px);
    }
  }
}
</style>
